package createThread.cn.tedu.threaddemo.thead;

import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName() + " is running...");
                } catch (InterruptedException e) {
                    System.out.println(Thread.currentThread().getName() + " 被中断了，准备停止运行...");
                    break; // 退出循环
                }
            }
        }, "线程1");

        thread.start();

        System.out.println("输入数字0 使得 线程1 停止");
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();

        if (num == 0) {
            thread.interrupt();  // 正确地中断线程1
        }

        System.out.println("线程1已停止");
    }
}
